package com.thkj.modules.user.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thkj.common.util.DateUtils;
import com.thkj.modules.dao.LivingAlarmMapper;
import com.thkj.modules.user.entity.LivingAlarm;
import com.thkj.modules.user.service.LivingAlarmService;

import dp.dao.PagingList;
import dp.service.BaseService;

@Service
public class LivingAlarmServiceImpl extends BaseService implements LivingAlarmService {

    @Autowired
    private LivingAlarmMapper livingAlarmMapper;

    @Override
    public void insert(LivingAlarm livingAlarm) {
        livingAlarmMapper.insert(livingAlarm);
    }

    @Override
    public PagingList<LivingAlarm> query(Integer page, Integer size, String individual_id, Integer alarm_state, Integer alarm_type, String device_id) {
        if (StringUtils.isBlank(individual_id)) {
            return new PagingList<>(0, null, 0, 0, 0);
        }
        PageHelper.startPage(page, size);
        Example example = new Example(LivingAlarm.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(individual_id)) {
            criteria.andEqualTo("individualId", individual_id);
        }
        if (StringUtils.isNotBlank(device_id)) {
            criteria.andEqualTo("deviceId", device_id);
        }
        if (alarm_state != null) {
            criteria.andEqualTo("alarmState", alarm_state);
        }

        if (alarm_type != null) {
            criteria.andEqualTo(" alarmType", alarm_type);
        }

        example.setOrderByClause("alarmTime DESC");

        List<LivingAlarm> violations = livingAlarmMapper.selectByExample(example);
        PageInfo<LivingAlarm> pageInfo = new PageInfo<>(violations);
        return new PagingList<LivingAlarm>(pageInfo.getTotal(), pageInfo.getList(), pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getPageSize());
    }

    /**
     * 查询总数
     *
     * @param individual_id
     * @param alarm_state
     * @param type
     * @param timeStr
     * @return
     */
    public Integer queryTotal(String individual_id, Integer alarm_state, int type, String timeStr) {
        int total = 0;
        if (((int) type) == 1) {
            total = livingAlarmMapper.queryByYearTotal(individual_id, alarm_state, timeStr);
        } else if (((int) type) == 2) {
            total = livingAlarmMapper.queryByMonthTotal(individual_id, alarm_state, timeStr);
        } else if (((int) type) == 3) {
            total = livingAlarmMapper.queryByDayTotal(individual_id, alarm_state, timeStr);
        } else {
            Example example = new Example(LivingAlarm.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotBlank(individual_id)) {
                criteria.andEqualTo("individualId", individual_id);
            }

            if (alarm_state != null) {
                criteria.andEqualTo("alarmState", alarm_state);
            }
            total = livingAlarmMapper.selectCountByExample(example);
        }
        return total;
    }

    @Override
    public Map<String, List<Object>> queryStatistics(String individual_id, Integer type, String timeStr) throws Exception {
        Map<String, List<Object>> maps = null;
        if (type == 1) {
            maps = queryByYear(individual_id, timeStr);
        }
        if (type == 2) {
            maps = queryByMonth(individual_id, timeStr);
        }

        if (type == 3) {
            maps = queryByWeek(individual_id);
        }

        if (type == 4) {
            maps = queryByDay(individual_id, timeStr);
        }
        return maps;
    }

    //根据年查询12月的
    @Override
    public Map<String, List<Object>> queryByYear(String individual_id, String year) {
        Map<String, List<Object>> mapItem = new HashMap<>();

        List<Map> maps = livingAlarmMapper.queryByYear(individual_id, year);
        if (maps == null) {
            return mapItem;
        }
        List<String> yesMonths = new ArrayList<>();
        for (Map map : maps) {
            yesMonths.add(map.get("nomth").toString());
        }
        for (int i = 1; i <= 12; i++) {
            if (!yesMonths.contains(i + "")) {
                Map<String, Integer> newMap = new HashMap<>();
                newMap.put("nomth", i);
                newMap.put("value", 0);
                maps.add(newMap);
            }
        }
        maps.sort(new Comparator<Map>() {
            @Override
            public int compare(Map o1, Map o2) {
                int i = Integer.parseInt(o1.get("nomth") + "") - Integer.parseInt(o2.get("nomth") + "");//先按照年龄排序
                if (i == 0) {
                    return Integer.parseInt(o1.get("nomth") + "") - Integer.parseInt(o2.get("nomth") + "");//如果年龄相等了再用分数进行排序
                }
                return i;
            }
        });

        List<Object> arrx = new ArrayList<>();
        List<Object> arry = new ArrayList<>();
        for (Map map : maps) {
            arrx.add(Integer.parseInt(map.get("nomth").toString()));
            arry.add(Integer.parseInt(map.get("value").toString()));
        }

        mapItem.put("arrx", arrx);
        mapItem.put("arry", arry);

        return mapItem;
    }

    /**
     * 按月查看 每天
     *
     * @param individual_id
     * @param month
     * @return
     */
    @Override
    public Map<String, List<Object>> queryByMonth(String individual_id, String month) throws ParseException {
        List<Map> maps = livingAlarmMapper.queryByMonth(individual_id, month);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        int dayOfMonth = DateUtils.getDaysOfMonth(sdf.parse(month));
        System.out.println(dayOfMonth);

        List<String> yesDays = new ArrayList<>();
        for (Map map : maps) {
            yesDays.add(map.get("day").toString());
        }
        for (int i = 1; i <= dayOfMonth; i++) {
            if (!yesDays.contains(i + "")) {
                Map<String, Integer> newMap = new HashMap<>();
                newMap.put("day", i);
                newMap.put("value", 0);
                maps.add(newMap);
            }
        }
        maps.sort(new Comparator<Map>() {
            @Override
            public int compare(Map o1, Map o2) {
                int i = Integer.parseInt(o1.get("day") + "") - Integer.parseInt(o2.get("day") + "");//先按照年龄排序
                if (i == 0) {
                    return Integer.parseInt(o1.get("day") + "") - Integer.parseInt(o2.get("day") + "");//如果年龄相等了再用分数进行排序
                }
                return i;
            }
        });
        List<Object> arrx = new ArrayList<>();
        List<Object> arry = new ArrayList<>();
        for (Map map : maps) {
            arrx.add(Integer.parseInt(map.get("day").toString()));
            arry.add(Integer.parseInt(map.get("value").toString()));
        }

        Map<String, List<Object>> mapItem = new HashMap<>();
        mapItem.put("arrx", arrx);
        mapItem.put("arry", arry);

        return mapItem;
    }

    @Override
    public Map<String, List<Object>> queryByDay(String individual_id, String day) {
        List<Map> maps = livingAlarmMapper.queryByDay(individual_id, day);
        List<String> yesDays = new ArrayList<>();
        for (Map map : maps) {
            yesDays.add(map.get("hour").toString());
        }
        for (int i = 1; i <= 24; i++) {
            if (!yesDays.contains(i + "")) {
                Map<String, Integer> newMap = new HashMap<>();
                newMap.put("hour", i);
                newMap.put("value", 0);
                maps.add(newMap);
            }
        }
        maps.sort(new Comparator<Map>() {
            @Override
            public int compare(Map o1, Map o2) {
                int i = Integer.parseInt(o1.get("hour") + "") - Integer.parseInt(o2.get("hour") + "");//先按照年龄排序
                if (i == 0) {
                    return Integer.parseInt(o1.get("hour") + "") - Integer.parseInt(o2.get("hour") + "");//如果年龄相等了再用分数进行排序
                }
                return i;
            }
        });
        List<Object> arrx = new ArrayList<>();
        List<Object> arry = new ArrayList<>();
        for (Map map : maps) {
            arrx.add(Integer.parseInt(map.get("hour").toString()));
            arry.add(Integer.parseInt(map.get("value").toString()));
        }

        Map<String, List<Object>> mapItem = new HashMap<>();
        mapItem.put("arrx", arrx);
        mapItem.put("arry", arry);

        return mapItem;
    }

    @Override
    public Map<String, List<Object>> queryByWeek(String individual_id) {
        List<Map> maps = livingAlarmMapper.queryByWeek(individual_id);
        List<String> yesWeeks = new ArrayList<>();
        for (Map map : maps) {
            yesWeeks.add(map.get("week").toString());
        }
        for (int i = 1; i <= 7; i++) {
            if (!yesWeeks.contains(i + "")) {
                Map<String, Integer> newMap = new HashMap<>();
                newMap.put("week", i);
                newMap.put("value", 0);
                maps.add(newMap);
            }
        }
        maps.sort(new Comparator<Map>() {
            @Override
            public int compare(Map o1, Map o2) {
                int i = Integer.parseInt(o1.get("week") + "") - Integer.parseInt(o2.get("week") + "");//先按照年龄排序
                if (i == 0) {
                    return Integer.parseInt(o1.get("week") + "") - Integer.parseInt(o2.get("week") + "");//如果年龄相等了再用分数进行排序
                }
                return i;
            }
        });

        List<Map> collect = maps.stream().map(map -> {
            int week = Integer.parseInt(map.get("week").toString());
            String weekStr = "";
            switch (week) {
                case 1:
                    weekStr = "周日";
                    map.put("weekStr", weekStr);
                    break;
                case 2:
                    weekStr = "周一";
                    map.put("weekStr", weekStr);
                    break;
                case 3:
                    weekStr = "周二";
                    map.put("weekStr", weekStr);
                    break;
                case 4:
                    weekStr = "周三";
                    map.put("weekStr", weekStr);
                    break;
                case 5:
                    weekStr = "周四";
                    map.put("weekStr", weekStr);
                    break;
                case 6:
                    weekStr = "周五";
                    map.put("weekStr", weekStr);
                    break;
                case 7:
                    weekStr = "周六";
                    map.put("weekStr", weekStr);
                    break;
            }
            return map;
        }).collect(Collectors.toList());

        List<Object> arrx = new ArrayList<>();
        List<Object> arry = new ArrayList<>();
        for (Map map : maps) {
            arrx.add(map.get("weekStr").toString());
            arry.add(Integer.parseInt(map.get("value").toString()));
        }

        Map<String, List<Object>> mapItem = new HashMap<>();
        mapItem.put("arrx", arrx);
        mapItem.put("arry", arry);

        return mapItem;

    }
}